//
//  SAModifyPasswordVC.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/9.
//
#import "SAModifyPasswordVC.h"
#import "SAUpdatePasswordAPI.h"
#import "NSString+MD5.h"
#import "SALoginVC.h"

@interface SAModifyPasswordVC ()<UITextFieldDelegate,HTTPAPIDelegate>

@property (nonatomic, strong) UIButton *finishBtn;
@property (nonatomic, strong) NSArray *titleArr;

@property (nonatomic, strong) NSString *oldPassWord;
@property (nonatomic, strong) NSString *passWord;
@property (nonatomic, strong) NSString *confirmPassWord;

@property (nonatomic, strong) SAUpdatePasswordAPI *updatePasswordAPI;

@end

@implementation SAModifyPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.updatePasswordAPI == api) {
        [SAValueManger shareInStance].userInfo = nil;
        [kUserDefault setObject:@"" forKey:kPasswordKey];
        UIWindow *window =  [UIApplication sharedApplication].keyWindow;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[SALoginVC new]];
        window.rootViewController = nav;
        [window makeKeyAndVisible];
        [Prompt popPromptViewWithMsg:@"密码已变更,请重新登录" duration:4];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
   
}

#pragma mark - event

- (void)clickfinishBtn {
    [self.view endEditing:YES];
    if (self.oldPassWord.length == 0) {
        [Prompt popPromptViewWithMsg:@"原密码不能为空" duration:2];
        return;
    }else if (self.passWord.length == 0) {
        [Prompt popPromptViewWithMsg:@"新密码不能为空" duration:2];
        return;
    }else if (self.confirmPassWord.length == 0) {
        [Prompt popPromptViewWithMsg:@"确认密码不能为空" duration:2];
        return;
    }
    if(!(trimString(self.passWord).length >=6 &&trimString(self.passWord).length <=20)){
        [Prompt popPromptViewWithMsg:@"请输入6-20 位数字，字母组合密码 !" duration:2];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"newPassWord"] = [NSString stringToMD5:trimString(self.passWord)];
    dict[@"oldPassWord"] = [NSString stringToMD5:trimString(self.oldPassWord)];
    self.updatePasswordAPI.coverParameters = dict;
    [self.updatePasswordAPI sendRequestToServerWithActivityViewAndMask:POST];
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == 0) {
        self.oldPassWord = textField.text;
    }else if (textField.tag == 1) {
        self.passWord = textField.text;
    }else if (textField.tag == 2) {
        self.confirmPassWord = textField.text;
    }
}

#pragma mark - event
-(void)buttonClick:(UIButton *)sender {
    
}

-(void)buttonClick2:(UIButton *)sender {
    
}

#pragma mark - initUI

- (void)initUI {
    
    self.view.backgroundColor = kLineColor;
    [self setNavigationItemBackBBIAndTitle:@"修改密码"];
    [self setBgImage:nil];
    
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        UIView *container = [[UIView alloc] init];
        container.backgroundColor = [UIColor whiteColor];
        container.layer.cornerRadius = 2.0;
        [self.view addSubview:container];
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kScaleNum(StatusHeight) + 25 + (40 + 10) * i);
            make.left.mas_equalTo(47.5);
            make.right.mas_equalTo(-47.5);
            make.height.mas_equalTo(40);
        }];
        
        UILabel *leftLab = [[UILabel alloc] init];
        leftLab.textColor = kBlackColor;
        leftLab.font = [UIFont systemFontOfSize:16.0];
        NSString *title = self.titleArr[i];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title];
        [attributedString addAttribute:NSForegroundColorAttributeName value:kRedColor range:[title rangeOfString:@"*"]];
        leftLab.attributedText = attributedString;
        [leftLab setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        [container addSubview:leftLab];
        [leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.centerY.mas_equalTo(0);
        }];
        
        UITextField *textField = [[UITextField alloc] init];
        textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"必填" attributes:@{ NSForegroundColorAttributeName: kLightgGrayColor, NSFontAttributeName: [UIFont systemFontOfSize:15.0] }];
        textField.textAlignment = NSTextAlignmentRight;
        textField.secureTextEntry = YES;
        textField.font = [UIFont systemFontOfSize:16.0];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.tag = i;
        textField.delegate = self;
        [container addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(0);
            make.left.equalTo(leftLab.mas_right).with.offset(10);
        }];
    }
    
    [self.view addSubview:self.finishBtn];
    [self.finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kScaleNum(StatusHeight) + 200);
        make.left.mas_equalTo(47.5);
        make.right.mas_equalTo(-47.5);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark - lazy init

- (UIButton *)finishBtn {
    if (!_finishBtn) {
        _finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_finishBtn setBackgroundImage:[UIImage imageNamed:@"button_blue_bg"] forState:UIControlStateNormal];
        _finishBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
        [_finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
        _finishBtn.layer.cornerRadius = 2;
        
        [_finishBtn addTarget:self action:@selector(clickfinishBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _finishBtn;
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"原密码*", @"新密码*", @"确认新密码*"];
    }
    return _titleArr;
}

-(SAUpdatePasswordAPI *)updatePasswordAPI {
    if (!_updatePasswordAPI) {
        _updatePasswordAPI = [SAUpdatePasswordAPI apiWithDelegate:self];
    }
    return _updatePasswordAPI;
}

@end
